package dsx.monitor.central;

import java.util.ArrayList;

import dsx.config.Config;
import dsx.config.ConfigItem;
import dsx.console.DsxConsole;
import dsx.dsxFrame.RPCServer;
import dsx.dsxFrame._RPCServer;
import dsx.log.ServLog;

public class Monitor {

	static RPCServer monitorServer = null;

	// 初始化（启动）监控服务
	public static void init() throws Exception {
		_RPCServer monitorConfig = loadMonitorConfig();
		if (monitorConfig != null) {
			monitorServer = new RPCServer();
			monitorServer.init(monitorConfig);
			if (monitorServer.getStatus() != 1)
				monitorServer.start();
		}
	}

	// 加载服务配置
	private static _RPCServer loadMonitorConfig() {
		try {
			ArrayList<ConfigItem> fields = Config.get("MonitorServer");
			_RPCServer config = new _RPCServer();
			for (int j = 0; j < fields.size(); j++) {
				ConfigItem field = fields.get(j);
				switch (field.name) {
				case "path":
					config.setPath(field.value);
					break;
				case "bizServerName":
					config.setBizServerName(field.value);
					break;
				case "netServerName":
					config.setNetServerName(field.value);
					break;
				case "bizDebugEnabled":
					if (field.value.equals("1"))
						config.setBizDebugEnabled(true);
					else
						config.setBizDebugEnabled(false);
					break;
				case "bizServerClass":
					config.setBizServerClass(field.value);
					break;
				case "converterClass":
					config.setConverterClass(field.value);
					break;
				case "netServerClass":
					config.setNetServerClass(field.value);
					break;
				case "slicerClass":
					config.setSlicerClass(field.value);
					break;
				case "maxWorkerNum":
					config.setMaxWorkerNum(Integer.parseInt(field.value));
					break;
				case "autoStart":
					if (field.value.equals("1"))
						config.setAutoStart(1);
					else
						config.setAutoStart(0);
					break;
				}
			}
			config.setNetServerConfig("");
			config.setStatus(0);
			return config;
		} catch (Exception ex) {
			DsxConsole.out(ex.getMessage(), "system", ServLog.error);
			return null;
		}

	}

}
